
package com.probiz.estore.imports.handler.product;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.probiz.estore.common.model.product.Product;
import com.probiz.estore.common.model.product.ProductSku;
import com.probiz.estore.common.model.product.ProductSkuOptionValue;
import com.probiz.estore.common.model.product.ProductTypeSkuOption;
import com.probiz.estore.common.model.product.SkuOption;
import com.probiz.estore.common.model.product.SkuOptionValue;
import com.probiz.estore.imports.handler.ColumnHandler;
import com.probiz.estore.imports.handler.basic.ColumnBasicHandler;
import com.probiz.estore.imports.model.Column;
import com.probiz.estore.imports.model.ImportModel;
import com.probiz.estore.product.service.ProdMainManager;
import com.probiz.estore.product.service.ProductManager;
import com.probiz.estore.product.service.SkuOptionValueManager;


public class SkuOptionHandler extends ColumnBasicHandler implements	ColumnHandler {
	private Logger logger = Logger.getLogger(SkuOptionHandler.class);
	/**
	 * 分隔符，如"=",尺码=XL;颜色=星月银
	 */
	private String equalDelimiter;
    private SkuOptionValueManager skuOptionValueManager ;
    private ProductManager productManager;
	private ProdMainManager prodMainManager;
	public void setProductManager(ProductManager productManager) {
		this.productManager = productManager;
	}
	public void setProdMainManager(ProdMainManager prodMainManager) {
		this.prodMainManager = prodMainManager;
	}
	public void setSkuOptionValueManager(SkuOptionValueManager skuOptionValueManager) {
		this.skuOptionValueManager = skuOptionValueManager;
	}

	public String getEqualDelimiter() {
		return equalDelimiter;
	}
	public void setEqualDelimiter(String equalDelimiter) {
		this.equalDelimiter = equalDelimiter;
	}
	
	public void setProperty(ImportModel importModel,Column column) throws Exception {
		Product product=(Product)importModel.getTarget();
		ProductSku defaultProductSku = product.getDefaultProductSku();
		try {
			List<SkuOptionValue> skuOptionValueList = new ArrayList<SkuOptionValue>();
			for(ProductTypeSkuOption productTypeSkuOption:product.getProductType().getProductTypeSkuOptions()){
				SkuOption skuOption = productTypeSkuOption.getSkuOption();
				for(String value:column.getValues()){
					String[] pair = value.split(equalDelimiter);
					if(skuOption.getSkuOptionName().equals(pair[0])){
						//找到所属的规格
						SkuOptionValue targetSkuOptionValue =null;
						for(SkuOptionValue skuOptionValue:skuOption.getSkuOptionValues()){
							if(skuOptionValue.getSkuOptionValueName().equals(pair[1])){
								//找到所属的规格值
								targetSkuOptionValue = skuOptionValue;
								skuOptionValueList.add(targetSkuOptionValue);
								break;
							}
						}
						if(null == targetSkuOptionValue){
							importModel.setResult(ImportModel.RESULT_ERROR);
							String msg = "无法导入,因为在规格【"+skuOption.getSkuOptionName()+"】下找不到规格值【"+pair[1]+"】";
							logger.error(msg);
							importModel.setMsg(msg);
							return;
						}
						break; //跳出到下个
					}
				}
			}
			//start:校验与检查
			if(defaultProductSku!=null && defaultProductSku.getProductSkuOptionValues().size()!=skuOptionValueList.size()){
				//设置结果
				importModel.setResult(ImportModel.RESULT_ERROR);
				String msg = "无法导入,因为货品规格选项与默认货品规格选项个数不匹配，请检查，修改商品时候要对原规格进行更改建议请通过后台界面进行操作";
				logger.error(msg);
				importModel.setMsg(msg);
				return;
			}
			if(defaultProductSku!=null){
				//保证规格选项未改变
				Set<Integer> skuOptionIdSet = new HashSet<Integer>();
				for(ProductSkuOptionValue productSkuOptionValue :defaultProductSku.getProductSkuOptionValues()){
					skuOptionIdSet.add(productSkuOptionValue.getSkuOptionValue().getSkuOptionId());
				}
				boolean flag = true;
				for(SkuOptionValue skuOptionValue :skuOptionValueList){
					if(!skuOptionIdSet.contains(skuOptionValue.getSkuOptionId())){
						flag = false;
					}
				}
				if(!flag){
					//设置结果
					importModel.setResult(ImportModel.RESULT_ERROR);
					String msg = "无法导入,因为货品规格选项与默认货品规格选项不匹配，请检查，修改商品时候要对原规格进行更改建议请通过后台界面进行操作";
					logger.error(msg);
					importModel.setMsg(msg);
					return;
				}
			}
			StringBuffer sb = new StringBuffer();
			sb.append("");
			for(int j=0; j< skuOptionValueList.size(); j++){
				sb.append(skuOptionValueList.get(j).getSkuOptionId()).append(":").append(skuOptionValueList.get(j).getSkuOptionValueId()).append(";");
			}	
			String skuOptionValueInfo= sb.toString();
			for(ProductSku sku:product.getProductSkus()){
				if(sku.getSkuOptionValueInfo().equals(skuOptionValueInfo)){
					//找到一致的，需要确认现在的productSkuId是否一致
					if(product.getImportProductSku().getProductSkuId()==null 
						|| !product.getImportProductSku().getProductSkuId().equals(sku.getProductSkuId())){
						//设置结果
						importModel.setResult(ImportModel.RESULT_ERROR);
						String msg = "无法导入,因为已有其他货品使用该规格值，请检查，修改商品时候要对原规格进行更改建议请通过后台界面进行操作";
						logger.error(msg);
						importModel.setMsg(msg);
						return;
					}
				}
			}
			//end:  校验与检查
			
			//成功
			product.getImportProductSku().setImportSkuOptionValueInfo(skuOptionValueInfo);
			product.getImportProductSku().setImportSkuOptionValueList(skuOptionValueList);
			importModel.setResult(ImportModel.RESULT_SUCCESS);
			
		}catch (Exception e) {
			//设置结果
			e.printStackTrace();
			importModel.setResult(ImportModel.RESULT_ERROR);
			String msg = "无法导入,因为货品规格选项格式错误，请检查";
			logger.error(msg);
			importModel.setMsg(msg);
			return;
		}	
	}
	

}
